/**
 * 
 */
package pl.edu.agh.to;

import java.util.List;

import android.app.AlertDialog;
import android.os.Bundle;

/**
 * @author karulis
 * 
 */
public class CoursesActivity extends TemplateActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	int getLayoutResID() {
		return R.layout.courses;
	}

	@Override
	int getSpinnerID() {
		return R.id.courses_spinner;
	}

	@Override
	int getListViewID() {
		return R.id.courses_list;
	}

	@Override
	List<Object> listViewContent() {
		return getdBAccess().getParticipants(getSpinnerSelectedItem());
	}

	@Override
	List<Object> spinnerContent() {
		return getdBAccess().getAllCourses();
	}

	@Override
	protected int getMenuRes() {
		return R.menu.courses_menu;
	}

	@Override
	protected void attendChange() {
		setupList();
	}

	@Override
	protected void rateChange() {
		// do nothing
	}

	@Override
	protected void courseChange() {
		setupSpinner();
	}

	@Override
	protected void participantChange() {
		setupList();
	}

	@Override
	protected void scheduleChange() {
		// do nothing
	}
}

class CourseRemoveDialog extends RemoveDialog {
	CourseRemoveDialog(TemplateActivity activity) {
		super(activity);
	}

	@Override
	protected void action() {
		getdBAccess().removeCourse(getSpinnerItem());
	}

	@Override
	protected int getMessageId() {
		return R.string.courses_dialog_text;
	}

}

class CourseEditDialog extends CourseAddDialog {
	Course course;

	CourseEditDialog(TemplateActivity context) {
		super(context);
	}

	@Override
	protected int layoutId() {
		return R.layout.courses_dialog;
	}

	@Override
	protected void prepareDialog() {
		course = getdBAccess()
				.getCourse(getActivity().getSpinnerSelectedItem());
		getUpperTextEdit().setText(course.getName());
		getLowerTextEdit().setText(course.getGroup());
		getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
	}

	@Override
	protected String getBaseMessage() {
		return getContext().getResources()
				.getText(R.string.courses_edit_dialog_text).toString();
	}

	@Override
	protected void action() {
		getdBAccess().updateCourse(
				course,
				new Course(getUpperTextEdit().getText().toString(),
						getLowerTextEdit().getText().toString()));
	}

}

class CourseAddDialog extends AddDialog {

	@Override
	protected void prepareDialog() {
		getUpperTextEdit().setText("");
		getLowerTextEdit().setText("");
		getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
	}

	@Override
	protected String getBaseMessage() {
		return getContext().getResources()
				.getText(R.string.courses_add_dialog_text).toString();
	}

	CourseAddDialog(TemplateActivity context) {
		super(context);
	}

	@Override
	protected void action() {
		getdBAccess().addCourse(
				new Course(getUpperTextEdit().getText().toString(),
						getLowerTextEdit().getText().toString()));
	}

	@Override
	protected boolean enableButton() {
		return getUpperTextEdit().getText().toString().isEmpty();
	}

	@Override
	protected int layoutId() {
		return R.layout.courses_dialog;
	}

	@Override
	protected int upperTextEditId() {
		return R.id.course_name_edit;
	}

	@Override
	protected int lowerTextEditId() {
		return R.id.group_name_edit;
	}

}

class AddToCourseDialog extends AddToDialog {

	AddToCourseDialog(TemplateActivity activity) {
		super(activity);
	}

	/**
	 * @return
	 */
	@Override
	protected int getViewId() {
		return R.layout.add_to_course_dialog;
	}

	/**
	 * @return
	 */
	@Override
	protected List<Object> getSpinnerContent() {
		return getdBAccess().getAllParticipants();
	}

	@Override
	protected int getMessageId() {
		return R.string.courses_add_to_dialog_text;
	}

	/**
	 * @return
	 */
	@Override
	protected int getDialogSpinnerId() {
		return R.id.add_to_course_spinner;
	}

	@Override
	protected void action() {
		getdBAccess().addParticipantToCourse(getDialogSpinnerItem(),
				getSpinnerItem());
	}
}

class RemoveFromCourseDialog extends AddToDialog {

	RemoveFromCourseDialog(TemplateActivity activity) {
		super(activity);
	}

	/**
	 * @return
	 */
	@Override
	protected int getViewId() {
		return R.layout.remove_from_course_dialog;
	}

	/**
	 * @return
	 */
	@Override
	protected List<Object> getSpinnerContent() {
		return getdBAccess().getParticipants(getSpinnerItem());
	}

	@Override
	protected int getMessageId() {
		return R.string.courses_remove_from_dialog_text;
	}

	/**
	 * @return
	 */
	@Override
	protected int getDialogSpinnerId() {
		return R.id.remove_from_course_spinner;
	}

	@Override
	protected void action() {
		getdBAccess().removeParticipantFromCourse(getDialogSpinnerItem(),
				getSpinnerItem());
	}
}